package com.lemon.smartsafetynew.ui.fragment.ledger;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.ledger.LedgerStatisticsVo;
import com.lemon.smartsafetynew.net.services.SafeLedgerServices;
import com.lemon.smartsafetynew.ui.activity.ledger.LedgerPatrolDetailActivity;
import com.lemon.smartsafetynew.ui.widget.ProgressArcView;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.common.StringResUtil;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/8/14.
 */

public class LedgerPatrolBannerFragment extends BaseFragment {

    @Bind(R.id.ledger_title_tv)
    TextView ledgerTitleTv;
    @Bind(R.id.ledger_detail_tv)
    TextView ledgerDetailTv;
    @Bind(R.id.ledger_date_tv)
    TextView ledgerDateTv;

    @Bind(R.id.left_progress)
    ProgressArcView leftProgress;
    @Bind(R.id.left_one)
    TextView leftOne;
    @Bind(R.id.left_two)
    TextView leftTwo;
    @Bind(R.id.left_three)
    TextView leftThree;

    @Bind(R.id.center_progress)
    ProgressArcView centerProgress;
    @Bind(R.id.center_one)
    TextView centerOne;
    @Bind(R.id.center_two)
    TextView centerTwo;
    @Bind(R.id.center_three)
    TextView centerThree;

    @Bind(R.id.right_one)
    TextView rightOne;
    @Bind(R.id.right_two)
    TextView rightTwo;

    @Bind(R.id.main_layout)
    LinearLayout mainLayout;


    private int type = 1;   // 1: 安全巡查   2：安全隐患  3: 动态监测

    private User user;

    public static LedgerPatrolBannerFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt(KeyConstants.KEY_LEDGER_DATA_TYPE, type);

        LedgerPatrolBannerFragment fragment = new LedgerPatrolBannerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getInt(KeyConstants.KEY_LEDGER_DATA_TYPE);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_ledger_patrol_banner;
    }

    @Override
    protected void initViews() {
        initData();
        initPageView();
        new Thread(new Runnable() {
            @Override
            public void run() {
                requestData(type);
            }
        }).start();

    }


    @OnClick(R.id.ledger_detail_tv)
    public void onViewClicked() {
        Bundle bundle = new Bundle();
        bundle.putInt(KeyConstants.KEY_LEDGER_DATA_TYPE, type);
        UIHelper.showActivity(getmContext(), bundle, LedgerPatrolDetailActivity.class);

    }

    private void initData() {
        user = AccountUtil.getUser();
        if (user == null) {
            Toast.makeText(getmContext(), "用户不存在", Toast.LENGTH_LONG).show();
        }
    }

    private void initPageView() {

//        switch (type) {
//            case 1: // 安全巡查
//                mainLayout.setBackgroundColor(Color.parseColor(ColorUtil.ledgerItemColor[type]));
//                break;
//            case 2: // 安全隐患
//                mainLayout.setBackgroundColor(Color.parseColor(ColorUtil.ledgerItemColor[type]));
//                break;
//            case 3: // 动态监测
//                mainLayout.setBackgroundColor(Color.parseColor(ColorUtil.ledgerItemColor[type]));
//                break;
//            default:
//                break;
//        }

        ledgerTitleTv.setText(StringResUtil.ledgerBannerString[type]);
        ledgerDateTv.setText(CommonUtil.getNowTimeStr(DateTypeEnum.DAY));
    }

    private void requestData(int type) {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", user.getCompanyId());
        params.put("type", "day"); //这里只需要Day
        params.put("date", ledgerDateTv.getText().toString().trim());
        params.put("moreInfo", 0);

        switch (type) {
            case 1: // 安全巡查
                SafeLedgerServices.init().getSafePatrolDetailData(params);
                break;
            case 2: // 安全隐患

                break;
            case 3: // 动态监测
                break;
            default:
                break;
        }
    }

    private void initContentView(LedgerStatisticsVo statisticsVo) {

//        leftProgress.setProgress(hashMap.get("item_total_num"), hashMap.get("item_checked_num"));
//        leftOne.setText("总巡查点：" + hashMap.get("item_total_num"));
//        leftTwo.setText("已巡查点：" + hashMap.get("item_checked_num"));
//        leftThree.setText("未巡查点：" + (hashMap.get("item_total_num") -  hashMap.get("item_checked_num")));
//
//        centerProgress.setProgress(hashMap.get("task_total_num"), hashMap.get("task_done_num"));
//        centerOne.setText("全部任务：" + hashMap.get("task_total_num"));
//        centerTwo.setText("已执行任务：" + hashMap.get("task_done_num"));
//        centerThree.setText("未执行任务：" + (hashMap.get("task_to_do_num") + hashMap.get("task_doing_num") + hashMap.get("task_over_time_num")));
//
//        rightOne.setText(""+hashMap.get("red_num"));

        leftProgress.setProgress(statisticsVo.getItem_total_num(), statisticsVo.getItem_checked_num());
        leftOne.setText("总巡查点：" + statisticsVo.getItem_total_num());
        leftTwo.setText("已巡查点：" + statisticsVo.getItem_checked_num());
        leftThree.setText("未巡查点：" + (statisticsVo.getItem_total_num() - statisticsVo.getItem_checked_num()));

        centerProgress.setProgress(statisticsVo.getTask_total_num(), statisticsVo.getTask_done_num());
        centerOne.setText("全部任务：" + statisticsVo.getTask_total_num());
        centerTwo.setText("已执行任务：" + statisticsVo.getTask_done_num());
        centerThree.setText("未执行任务：" + (statisticsVo.getTask_to_do_num()
                + statisticsVo.getTask_doing_num() + statisticsVo.getTask_over_time_num()));

        rightOne.setText("" + statisticsVo.getRed_num());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_LEDGER_STATISTICS_OF_SAFE_PATROL.equals(event.getTag())) {

            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                //mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                //mHUDUtil.hidden();

                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    LedgerStatisticsVo statisticsVo = (LedgerStatisticsVo) event.getObj();
                    initContentView(statisticsVo);

                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        OkHttpUtils.getInstance().cancelTag(KeyConstants.URL_GET_SAFE_LEDGER_STATISTICS_OF_SAFE_PATROL);
    }
}